import sqlite3 as lite
import sys

def chkDB():
    chessboard = open("chessboard.db","r")
    chk = False
    if chessboard:
        chessboard.close()
        chk = True
    else:
        chk = False
    return chk
    

def SetupDB():
    con = None
    if not chkDB():
        try:
            con = lite.connect('chessboard.db')
            
            cur = con.cursor()
            cmd = "create table Chessboard (colour,piece,Z,X,Y)"    
            cur.execute(cmd)
            
            data = cur.fetchone()
            
            print "SQLite version: %s" % data
        except lite.Error, e:
            
            print "Error %s:" % e.args[0]
            sys.exit(1)
            
        finally:
            
            if con:
                con.close()
    return True

def InsertKnight():
    con = lite.connect('chessboard.db')        
    cur = con.cursor()
    #cmd = "INSERT INTO Chessboard(colour,piece,Z,X,Y) VALUES ('Black','BKnight1','1','1','1')"    
    with con:
        cur.execute("INSERT INTO Chessboard VALUES('Black','BKnight1','1','1','1')")
    if con:
        con.close()
    return True

def getCoordinates(Piece):
    con = lite.connect('chessboard.db')
        
    cur = con.cursor()
    cmd1 = 'Select Z,X,Y Where piece=?',(Piece)
    
    cur.execute("SELECT Z,X,Y FROM Chessboard WHERE piece=(?)",(Piece,))
    #cur.execute("SELECT Z,X,Y FROM Chessboard WHERE piece='BKnight1'")
    data = cur.fetchall()
    for data2 in data:
        print data2
    return True
        
SetupDB()
InsertKnight()
getCoordinates("BKnight1")